package com.bst.datax.executor.core.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

import javax.sql.DataSource;
import java.util.Base64;

/**
 * 缺乏doris环境无法公开测试，请各位自行开放该方案
 */
// @Configuration
public class DorisConfig {

    @Value("${doris.fe.host:localhost}")
    private String feHost;

    @Value("${doris.fe.http-port:8030}")
    private int feHttpPort;

    @Value("${doris.jdbc.url}")
    private String jdbcUrl;

    @Value("${doris.jdbc.username}")
    private String username;

    @Value("${doris.jdbc.password}")
    private String password;

    @Bean
    public DataSource dorisDataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(jdbcUrl);
        config.setUsername(username);
        config.setPassword(password);
        return new HikariDataSource(config);
    }

    @Bean
    public WebClient dorisWebClient() {
        return WebClient.builder()
                .baseUrl("http://" + feHost + ":" + feHttpPort)
                .defaultHeader("Authorization", "Basic " +
                        Base64.getEncoder().encodeToString((username + ":" + password).getBytes()))
                .build();
    }
}
